package com.sgb.member.view_model;

import com.sgb.member.MemberConfig;
import com.sgb.member.di.repository.MemberRepertory;
import com.sgb.member.entity.ClientPerformanceBean;
import com.sgb.member.entity.ListMResponse;
import com.sgb.member.ui.activity.ClientPerformanceActivity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.view.selection.CommonPopEntity;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;

/**
 * Created by：冯齐 on 2021/1/26 16:56
 * Description：
 */
public class ClientPerformanceModel extends BaseViewModel {

    ClientPerformanceActivity activity;
    MemberRepertory repertory;

    private int current;

    public ClientPerformanceModel(ClientPerformanceActivity activity, MemberRepertory repertory) {
        this.activity = activity;
        this.repertory = repertory;
    }

    public void getPerformances(boolean isLoadMore, String searchTxt, String no1, String no3) {
        int currentPage = isLoadMore ? current + 1 : 1;
        Map<String, Object> map = new HashMap<>();
        map.put("type", no1); //业务类型
        map.put("status", no3);  // 项目状态
        map.put("name", searchTxt); // 搜索名称和业绩名称
        map.put("limit", 20);
        map.put("page", currentPage);
        DialogHelper.showProgressDialog(activity, null, "数据获取中...", 0, false, null).setCanceledOnTouchOutside(false);
        repertory.getApi().getMemberCertificate(map).enqueue(new ICallBack<BaseEntity<ListMResponse<List<ClientPerformanceBean>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListMResponse<List<ClientPerformanceBean>>> data) {
                current = data.getData().getCurrent();
                int total = data.getData().getPages();
                activity.refreshRecycler(data.getData().getList(), isLoadMore, current >= total);
            }

            @Override
            public void onFailure(Call<BaseEntity<ListMResponse<List<ClientPerformanceBean>>>> call, Throwable t) {
                super.onFailure(call, t);
                activity.showEmpty();
            }
        });
    }

    // 会员业绩业务类型
    public void getMemberPerformanceTypeList(boolean isShowPopup) {
        DialogHelper.showProgressDialog(activity, null, "数据获取中...", 0, false, null).setCanceledOnTouchOutside(false);
        repertory.getApi().getMemberPerformanceTypeList().enqueue(new ICallBack<BaseEntity<List<CommonPopEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<CommonPopEntity>> data) {
                activity.setMemberPerformanceTypeList(isShowPopup, data.getData());
            }
        });
    }

    // 会员业绩项目状态
    public void getProjectStatus(boolean isShowPopup) {
        DialogHelper.showProgressDialog(activity, null, "数据获取中...", 0, false, null).setCanceledOnTouchOutside(false);
        repertory.getApi().getMemberPerformanceStatusList().enqueue(new ICallBack<BaseEntity<List<CommonPopEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<CommonPopEntity>> data) {
                activity.setProjectStatus(isShowPopup, data.getData());
            }
        });
    }



    public void getUrlSetting(String param, String id) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        repertory.getApi().getUrlSetting(param).enqueue(new ICallBack<BaseEntity<H5ReEntity>>() {
            @Override
            public void onSuccess(BaseEntity<H5ReEntity> data) {
                String url = data.getData().getUrl() + "?id=" + id;
                MemberConfig.getInstance().startWebViewX5(activity, url, 101);
            }
        });
    }
}
